@extends("layouts.common")
@section("content")

    <style>
        .panel .panel-heading .panel-actions {
            float: right;
            margin-right: -15px;
            height: 40px;
            font-size: 0;
            width: 30%;
            text-align: right;
        }
    </style>
    <div class="row">
        <div class="col-lg-12">
            @if (isset($errors) && count($errors) > 0)
                <div class="alert alert-danger alert-dismissible">
                    <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                    <h4><i class="icon fa fa-ban"></i> 错误</h4>
                    <ul>
                        @foreach ($errors->all() as $error)
                            <li>{{ $error }}</li>
                        @endforeach
                    </ul>
                </div>
            @endif
            @if (Session::has('messages'))
                <div class="alert alert-success alert-dismissible">
                    <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                    <h4><i class="icon fa fa-check"></i> 成功!</h4>
                    <ul>
                        @foreach (Session::get('messages') as $error)
                            <li>{{ $error }}</li>
                        @endforeach
                    </ul>
                </div>
            @endif
        </div>
    </div>

    <div class="row">
        <div class="col-lg-12">
            <h3 class="page-header"><i class="fa fa-table"></i>代理管理</h3>
            <ol class="breadcrumb">
                <li><i class="fa fa-home"></i><a href="/welcome">Home</a></li>
                <li><i class="fa fa-table"></i>代理列表</li>
            </ol>
        </div>
    </div>

    <div class="row">
        <div class="col-lg-12">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h2><i class="fa fa-indent red"></i><strong>搜索</strong></h2>
                </div>
                <form action="" method="get" class="form-inline">
                    <div class="panel-body">

                        <div class="form-group">
                            <label class="sr-only" for="if-password">姓名</label>
                            <input type="text" id="if-password" name="name" class="form-control" placeholder="姓名">
                        </div>
                        <div class="form-group">
                            <label class="sr-only" for="if-email">Email</label>
                            <input type="text" id="if-email" name="email" class="form-control" placeholder="邮箱">
                        </div>
                        <div class="form-group">
                            <label class="sr-only" for="if-email">Email</label>
                            <input type="text" id="if-email" name="phone" class="form-control" placeholder="手机号">
                        </div>
                        <div class="form-group">
                            <select id="select" name="needpay" class="form-control" size="1">
                                <option value="">是否结算</option>
                                <option value="1">需结算</option>
                            </select>
                        </div>
                    </div>
                    <div class="panel-footer">
                        <button type="submit" class="btn btn-sm btn-success"><i class="fa fa-dot-circle-o"></i> 搜索</button>
                        <button type="reset" class="btn btn-sm btn-danger"><i class="fa fa-ban"></i> 重置</button>
                    </div>
                </form>
            </div>

        </div>
    </div>

    <div class="row">

        <div class="col-lg-12">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h2><i class="fa fa-table red"></i><span class="break"></span><strong>代理列表</strong></h2>

                    {{--<div class="panel-actions">--}}
                        {{--<a href="/agent/add" style=""><i class="fa  fa-table"></i>交易记录&nbsp;&nbsp;</a>--}}
                        {{--<a href="/agent/add"><i class="fa  fa-plus"></i>添加代理&nbsp;&nbsp;</a>--}}
                    {{--</div>--}}
                </div>
                <div class="panel-body">
                    <table class="table table-condensed">
                        <thead>
                        <tr>
                            <th>姓名</th>
                            <th>邮箱</th>

                            <th>手机</th>
                            <th>银行卡号</th>
                            <th>提成</th>
                            <th>余额</th>
                            <th>交易金额</th>
                            <th>留存比</th>
                            <th>状态</th>
                            <th>操作</th>
                            {{--<th>上级</th>--}}
                        </tr>
                        </thead>
                        <tbody>
                        @foreach($data as $val)
                        <tr>
                            <td>{{$val->name}}</td>
                            <td>{{$val->email}}</td>

                            <td>{{$val->phone}}</td>
                            <td>{{$val->bankcard}}</td>
                            <td>{{$val->percent}}%</td>
                            <td>{{$val->money}}</td>
                            <td>{{$val->totlemoney}}</td>
                            <td class="tranid" data-id="{{$val->id}}">{{$val->remain}}</td>
                            <td>@if($val->act == 0)已禁用@else已开启@endif</td>
                            <td>
                                @if($val->upgrade=='1')
                                    <a class="btn btn-primary  btn-lg active upgrade btn-sm" data-toggle="modal" data-target="#upgrade"  data-val="{{$val->id}}"> 升级</a>
                                @endif
                                     <a class="btn btn-primary  btn-lg active btn-sm" href="/agent/agentlist/{{$val->id}}"  data-val="{{$val->id}}"> 查看</a>
                                @if($val->act == 1)
                                     <a class="btn btn-danger btn-lg active btn-sm btn-delete" data-toggle="modal" data-target="#myModal" data-val="{{$val->id}}">禁用</a>
                                @else
                                     <a class="btn btn-lg active btn-sm btn-info btn-delete1" data-toggle="modal" data-target="#myModal1" data-val1="{{$val->id}}">开启</a>
                                 @endif

                                @if($val->money>30 && $val->bankcard !='')
                                        <a href="/agent/pay/{{$val->id}}"  class="btn btn-primary  btn-lg active upgrade btn-sm">结算</a>
                                @endif

                            </td>
                            {{--<td>{{$val->pname}}</td>--}}
                        </tr>
                            @endforeach

                        </tbody>
                    </table>
                    @include("layouts.page",['paginator'=>$data])
                </div>
            </div>
        </div><!--/col-->
    </div>
    <style>
        td,th{
            text-align: center;
        }
    </style>
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">警告</h4>
                </div>
                <div class="modal-body">
                    你确定要禁用么？
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <a  class="btn btn-danger active btn-sm model_delete" data-val="" role="button">确定</a>
                </div>
            </div>
        </div>
    </div>
    <div class="modal fade" id="myModal1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">警告</h4>
                </div>
                <div class="modal-body">
                    你确定要开启么？
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <a  class="btn btn-info active btn-sm model_delete" data-val="" role="button">确定</a>
                </div>
            </div>
        </div>
    </div>
    <div class="modal fade" id="upgrade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">警告</h4>
                </div>
                <div class="modal-body">
                    你确定要升级代理吗？
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <a  class="btn btn-info active btn-sm model_delete" data-val="" role="button">确定</a>
                </div>
            </div>
        </div>
    </div>
    <script>
        $(function () {
            $(".btn-delete").click(function(){
                $("#myModal").find('.modal-footer .btn-danger').attr('href', "/agent/close/" + $(this).attr("data-val"));
            });
            $(".btn-delete1").click(function(){
                $("#myModal1").find('.modal-footer .btn-info').attr('href', "/agent/open/" + $(this).attr("data-val1"));
            });
            $(".upgrade").click(function(){
                $("#upgrade").find('.modal-footer .btn-info').attr('href', "/agent/upgrade/" + $(this).attr("data-val"));
            });


            $(".tranid").click(function () {
                var _this=$(this);

                var text= $(this).text();
                var input= $(this).find("input").val();
                if(typeof(input) == "undefined"){
                    var val=text;
                }else{
                    var val=input;
                }
                var id= $(this).data("id");

                $(this).html("<input value='"+val+"'>");
                $(this).find("input").focus();
                $(this).find("input").on('blur',function () {
                    var input= _this.find("input").val();
                    _this.text('');
                    if(text != input){


                        $.ajax({
                            url: '/agent/setremain',
                            async: false,
                            type: "post",
                            data: {'id':id,'val':input,'_token':"{{csrf_token()}}"},
                            dataType: "json",
                            error:function(){

                            },
                            success: function (data) {
                                if(data=='1'){
                                    _this.text(input);
                                }else if(data=='3'){
                                    alert("设置失败");
                                    _this.text(text);
                                }else {
                                    _this.text(text);
                                }

                            }
                        });
                    }else{
                        _this.text(text);
                    }

                });

            });


            var search = location.search.substring(1);
            if (search) {
                try {
                    var query = JSON.parse('{"' + decodeURIComponent(search).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g, '":"') + '"}');

                    $.each(query, function(k, v) {
                        $('[name=' + k + ']').val(v);
                    });
                } catch (e) {
                    console.log(e);
                }

            }
        });
    </script>
@endsection